<template>
  <div class="app-container">
    <div v-if="!warmReminderRecordShow">
      <el-form :model="warmReminderForm" :inline="true">
        <el-form-item label="适用专病" prop="diseaseId">
          <el-select v-model="warmReminderForm.diseaseId" filterable @change="changeWarmReminderList" placeholder="适用专病"  :clearable="false"
            clearable style="width: 160px">
            <el-option v-for="dict in sysManageDiseases" :key
              ="dict.diseaseClassifyId" :label="dict.diseaseClassifyName"
              :value="dict.diseaseClassifyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="type" clearable>
          <el-select v-model="warmReminderForm.type" placeholder="类别" filterable @change="changeWarmReminderList"
            clearable style="width: 160px">
            <el-option v-for="dict in dict.type.sys_textmanage_type" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="文本标题" prop="planId">
          <el-select v-model="warmReminderForm.textId" filterable @change="warmReminderPreview" placeholder="请选择文本标题"
            clearable style="width: 250px">
            <el-option v-for="(item, index) in warmReminderClassifyList" :key="index" :label="item.title"
              :value="item.textId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>

          <el-badge :value="warmReminderRecordParamsTotal">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getWarmReminderRecord">已提示记录</el-button>
          </el-badge>
        </el-form-item>

        <!--        <el-row>-->
        <!--          <el-col :span="7">-->
        <!--            <el-form-item label="类别" prop="type" clearable>-->
        <!--              <el-select v-model="warmReminderForm.type" placeholder="类别" clearable style="width: 160px">-->
        <!--                <el-option v-for="dict in dict.type.sys_textmanage_type" :key="dict.value" :label="dict.label"-->
        <!--                           :value="dict.value" />-->
        <!--              </el-select>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="7">-->
        <!--            <el-form-item label="适用专病" prop="diseaseId">-->
        <!--              <el-select v-model="warmReminderForm.diseaseId" @change="changeWarmReminderList" placeholder="适用专病"-->
        <!--                         clearable style="width: 160px">-->
        <!--                <el-option v-for="dict in sysManageDiseases" :key="dict.diseaseClassifyId"-->
        <!--                           :label="dict.diseaseClassifyName" :value="dict.diseaseClassifyId" />-->
        <!--              </el-select>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="10">-->
        <!--            <el-form-item label="文本标题" prop="planId">-->
        <!--              <el-select v-model="warmReminderForm.textId" @change="warmReminderPreview" placeholder="请选择文本标题" clearable style="width: 250px">-->
        <!--                <el-option v-for="(item, index) in warmReminderClassifyList" :key="index" :label="item.title"-->
        <!--                           :value="item.textId"></el-option>-->
        <!--              </el-select>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="6">-->
        <!--            <el-button type="primary" icon="el-icon-search" size="mini" @click="getWarmReminderRecord">已提示记录</el-button>-->

        <!--          </el-col>-->
        <!--        </el-row>-->

        <el-form-item class="warmReminderContentBox" v-if="warmReminderContent.content">
          <div v-html="warmReminderContent.content">
          </div>
        </el-form-item>
        <el-form-item label="个性提示" prop="remark">
          <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="warmReminderForm.selfTips"
            style="width: 500px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="display: flex;justify-content: center">
        <el-button v-if="warmReminderBtnStatus == '0'" @click="submitWarmReminderForm">提 交</el-button>
        <el-button v-if="warmReminderBtnStatus == '2'" @click="updateWarmReminderForm">修 改</el-button>
        <el-button @click="cancelWarmReminderForm">取 消</el-button>
      </div>
    </div>
    <div v-else class="formBox">
      <el-table border v-loading="loading" :data="warmReminderRecordData">
        <el-table-column label="序号" align="center" type="index" width="50">
          <template slot-scope="scope">
            <span>{{ getWarmReminderRecordIndex(scope.$index) }}</span>
          </template>
        </el-table-column>
        <el-table-column sortable align="center" prop="title" label="文本标题" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column sortable align="center" prop="selfTips" label="个性提示" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column sortable align="center" prop="nickName" label="创建医生" width="100"></el-table-column>
        <el-table-column sortable align="center" prop="createTime" label="创建日期" width="180"></el-table-column>

        <el-table-column align="center" fixed="right" label="操作" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-document" @click="lookRecordItem(scope.row)">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updateRecordItem(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteRecordItem(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="warmReminderRecordParamsTotal > 0" :total="warmReminderRecordParamsTotal"
        :page.sync="warmReminderRecordParams.pageNum" :limit.sync="warmReminderRecordParams.pageSize"
        @pagination="getWarmReminderRecord" />

    </div>
  </div>
</template>

<script>
import { listClassify } from "@/api/system/classify";
import {listText, listTextDept} from "@/api/system/text";
import { addTips, deleteTips, getTipsList, updateTips } from "@/api/warmReminder/warmReminder";

export default {
  name: "warmReminder",
  dicts: ["sys_textmanage_type"],
  props: ['baseInfo'],
  data() {
    return {
      // 遮罩层
      loading: false,
      warmReminderForm: {
        type: '2', //文本类别
        diseaseId: '',//专病类别
        textId: '',
        selfTips: '',
        status: '0'
      },
      sysManageDiseases: [],//专病管理
      // 专病表格数据
      warmReminderClassifyList: [],
      warmReminderContent: {},
      // 复诊记录显示
      warmReminderRecordShow: false,
      warmReminderRecordData: [],
      warmReminderRecordParams: {
        pageNum: 1,
        pageSize: 10,
        visitNum: '',
      },
      // 总条数
      warmReminderRecordParamsTotal: 0,
      warmReminderBtnStatus: '0',// '0':提交,'1':隐藏,'2'修改
      diagnosisList:[]
    }
  },
  methods: {
    // 真实序号计算（计算分页影响）
    getWarmReminderRecordIndex(index) {
      return (this.warmReminderRecordParams.pageNum - 1) * this.warmReminderRecordParams.pageSize + index + 1;
    },
    //温馨提示
    async warmReminder() {
      this.warmReminderForm.textId = ''
      this.warmReminderForm.selfTips = ''
      this.warmReminderContent = {}
      this.warmReminderBtnStatus = '0'
      this.getclassList()
      // this.getWarmReminderList()
      this.warmReminderRecordShow = false
    },
    // 获取专病列表
    getclassList() {
      let params = {
        pageNum: 1,
        pageSize: 9999,
        status: '0',
      }
      listClassify(
        params
      ).then((response) => {
        let data=response.rows;
        let data0=[]
        let data1=[]
        let dataAll=[]
        console.log(this.baseInfo.doctorDiagnosis)
        let doctorDiagnosis=this.baseInfo.doctorDiagnosis
        if(doctorDiagnosis){
          data.forEach(function(ele) {
             if(doctorDiagnosis.indexOf(ele.diseaseClassifyName)>-1){
              data0.push(ele)
             }else{
              data1.push(ele)
             }
         });
         dataAll=data0.concat(data1)
        }else{
          dataAll=data
        }

        this.sysManageDiseases = dataAll;
        this.warmReminderForm.diseaseId=dataAll[0].diseaseClassifyId
        this.getWarmReminderList()
      });
    },
    //适用专病改变
    changeWarmReminderList() {
      this.warmReminderClassifyList = []
      this.warmReminderForm.textId = ''
      this.getWarmReminderList()
    },
    /** 查询文本标题 */
    getWarmReminderList() {
      listTextDept({
        pageNum: 1,
        pageSize: 9999,
        diseaseId: this.warmReminderForm.diseaseId,
        type: this.warmReminderForm.type,
        status: this.warmReminderForm.status,
        deptId:this.baseInfo.deptId
      }).then((response) => {
        this.warmReminderClassifyList = response.rows;
        // console.log('getList', response)
      });
    },
    //温馨提示预览
    warmReminderPreview() {
      if (this.warmReminderForm.textId) {
        this.warmReminderContent = this.warmReminderClassifyList.find(item => {
          return item.textId == this.warmReminderForm.textId
        })
      } else {
        this.warmReminderContent = {}
      }
    },
    //获取已提醒记录角标
    getWarmReminderRecordcount() {
      this.loading = true
      this.warmReminderRecordParams.visitNum = this.baseInfo.visitNum
      getTipsList(this.warmReminderRecordParams).then(response => {
        if (response.code == 200) {
          this.loading = false
          this.warmReminderRecordData = response.rows
          this.warmReminderRecordParamsTotal = response.total
        } else {
          this.loading = false

        }
      })
    },
    //温馨提示记录
    getWarmReminderRecord() {
      this.warmReminderRecordShow = true
      this.loading = true
      this.warmReminderRecordParams.visitNum = this.baseInfo.visitNum
      getTipsList(this.warmReminderRecordParams).then(response => {
        if (response.code == 200) {
          this.loading = false
          this.warmReminderRecordData = response.rows
          this.warmReminderRecordParamsTotal = response.total
          listText({
            pageNum: 1,
            pageSize: 9999,
            diseaseId: ''
          }).then((res) => {
            this.warmReminderClassifyList = res.rows;
          });
        } else {
          this.loading = false

        }
      })
    },
    lookRecordItem(row) {
      console.log('lookRecordItem--', row)
      this.warmReminderForm = row
      // this.warmReminderForm.textId=row.textId
      // this.warmReminderForm.diseaseId=''
      // this.warmReminderForm.selfTips=row.selfTips
      this.warmReminderRecordShow = false
      this.warmReminderBtnStatus = '1'
      this.warmReminderContent = this.warmReminderClassifyList.find(item => {
        return item.textId == this.warmReminderForm.textId
      })
      console.log('warmReminderClassifyList---', this.warmReminderClassifyList)
      console.log('warmReminderContent---', this.warmReminderContent)

    },
    updateRecordItem(row) {
      this.warmReminderForm = row
      // this.warmReminderForm.textId=row.textId
      // this.warmReminderForm.diseaseId=''
      // this.warmReminderForm.selfTips=row.selfTips
      this.warmReminderRecordShow = false
      this.warmReminderBtnStatus = '2'
      this.warmReminderContent = this.warmReminderClassifyList.find(item => {
        return item.textId == this.warmReminderForm.textId
      })

    },
    deleteRecordItem(row) {
      this.loading = true
      deleteTips(row.tipsId).then(response => {
        if (response.code == 200) {
          this.getWarmReminderRecord()
          this.$message.success(response.msg)
          this.loading = false
        } else {
          this.loading = false
        }
      })
    },
    updateWarmReminderForm() {
      updateTips(this.warmReminderForm).then(response => {
        if (response.code == 200) {
          this.$message.success(response.msg)
          this.$emit('closeWarmReminderVisible')

        }
      })

    },
    submitWarmReminderForm() {
      let data = {
        visitNum: this.baseInfo.visitNum,
        patientId: this.baseInfo.patientId,
        doctorId: this.baseInfo.doctorId,
        textId: this.warmReminderForm.textId,
        selfTips: this.warmReminderForm.selfTips
      }
      // if (data.textId || data.selfTips) {
      if (data.textId) {
        addTips(data).then((response) => {
          if (response.code == 200) {
            this.$message.success(response.msg)
            this.$emit('closeWarmReminderVisible')
          }
        }
        )
      } else {
        // this.$message.warning('请选择适用专病的文本标题或者输入个性提示!')
        this.$message.warning('请选择适用专病的文本标题!')
      }

    },
    cancelWarmReminderForm() {
      this.$emit('closeWarmReminderVisible')
    },

  },
  mounted() {
    console.log('mounted —— 温馨提示', this.baseInfo)
    this.warmReminder()
    this.getWarmReminderRecordcount()
  }


}
</script>

<style scoped rel="stylesheet/scss"  lang="scss">
.warmReminderContentBox {
  ::v-deep table {
    border: 1px solid #ddd;
    border-collapse: collapse;
  }

  ::v-deep td {
    border: 1px solid #ddd;
    height: 40px;
    padding-left: 5px;
  }

  ::v-deep img {
    max-width: 100%;
  }
}
</style>
